# -*- coding:utf-8 -*-
from xml.dom.minidom import Document
from sklearn.cluster import KMeans
from Course.base_Course_data import *
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class Course_data:
    '''
    将课程分装成类进行操作
    '''
    def __init__(self,location):
        self.data=dealdate_xml(open_xml_data(config.ORIGINAL_DATA_XML_FILE_PATH))
    def setN(self,n):
        '''
        :param n: 所要分类的数目
        '''
        self.n=n
    def getData(self):
        '''
        :return: 返回原始数据
        '''
        return self.data
    def getDealdaMat(self):
        '''
        :return: 返回归一化数据
        '''
        return self.dealdaMat
    def changeWeight(self,position,weight1):
        '''
        :param position: 所要改变权重的位置
        :param weight1: 所要改变的权重
        :return: 返回改变权重的列表
        '''
        self.dealdaMat=weight(self.dealdaMat,position,weight1)
    def getN(self):
        return self.n
    def getMaxMinlist(self):
        '''
        :return: 放回原始数据的最大值列表和最小值列表
        '''
        max,min=max_and_min(self.getData())
        return max,min
    def cluster_kind(self):
        '''
        :return: 分类后的类别列表
        '''
        kmeans_data = KMeans(n_clusters=self.getN()).fit(self.getDealdaMat()).labels_
        return kmeans_data
    def cluster_ceters(self):
        '''
        :return: 分类后的类别属性
        '''
        kmeans_centers = KMeans(n_clusters=self.getN()).fit(self.getDealdaMat()).cluster_centers_
        return kmeans_centers
    def cluster_predict(self,X):
        '''
        :param X: 要预测的点的列表
        :return: 该点所在的类别
        '''
        x=X.reshape(1, -1)
        kmeans_predict=KMeans(n_clusters=self.getN()).fit(self.getDealdaMat()).predict(x)
        data=kmeans_predict[0]
        return data

# 将self.orderDict中的信息写入本地xml文件，参数filename是xml文件名

    def writeInfoToXml(self, filename):
        # 创建dom文档
        doc = Document()
        # 创建根节点
        orderlist = doc.createElement('crouses')
        # 根节点插入dom树
        doc.appendChild(orderlist)
        # 依次将orderDict中的每一组元素提取出来，创建对应节点并插入dom树
        for iter in self.data:
            for i in range(len(iter)):
                if iter[i]=='':
                    iter[i]='0'
            (number,name,english_name,college,credit,total_class_hours,
             theory_class_hours,experiment_period,computer_class_hours
                 ,practice_one,week_class_hours,semester) = (iter[0],iter[1],iter[2],iter[3],iter[4],iter[5],iter[6],iter[7],iter[8],iter[9],iter[10],iter[11])

            # 每一组信息先创建节点<order>，然后插入到父节点<orderlist>下
            order = doc.createElement('course')
            orderlist.appendChild(order)
            # 将姓名插入<order>中
            # 创建节点<customer>
            customer = doc.createElement('c_no')
            # 创建<customer>下的文本节点
            customer_text = doc.createTextNode(number)
            # 将文本节点插入到<customer>下
            customer.appendChild(customer_text)
            # 将<customer>插入到父节点<order>下
            order.appendChild(customer)
            # 将电话插入<order>中，处理同上
            phone = doc.createElement('chinese_name')
            phone_text = doc.createTextNode(name)
            phone.appendChild(phone_text)
            order.appendChild(phone)
            # 将地址插入<order>中，处理同上
            address = doc.createElement('english_name')
            address_text = doc.createTextNode(english_name)
            address.appendChild(address_text)
            order.appendChild(address)
            # 将点餐次数插入<order>中，处理同上
            count = doc.createElement('academy_no')
            count_text = doc.createTextNode(college)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('credit')
            count_text = doc.createTextNode(credit)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('class_hours')
            count_text = doc.createTextNode(total_class_hours)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('theory_hours')
            count_text = doc.createTextNode(theory_class_hours)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('experiment_hours')
            count_text = doc.createTextNode(experiment_period)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('computer_hours')
            count_text = doc.createTextNode(computer_class_hours)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('practice_hours')
            count_text = doc.createTextNode(practice_one)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('week_hours')
            count_text = doc.createTextNode(week_class_hours)
            count.appendChild(count_text)
            order.appendChild(count)
            count = doc.createElement('emester')
            count_text = doc.createTextNode(semester)
            count.appendChild(count_text)
            order.appendChild(count)

        # 将dom对象写入本地xml文件
        with open(filename, 'w') as f:
            f.write(doc.toprettyxml(indent=''))
        return

def main():
    str=u'course_utf8'
    course=Course_data(str)
    course.writeInfoToXml('course.xml')
if __name__ == '__main__':
    main()
